package zhr.chapter9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditWindow extends JFrame implements ActionListener {
    private JMenuBar menubar;
    private JMenu menu;
    private JMenuItem itemCopy,itemCut,itemPaste;
    private JTextArea text;
    EditWindow() {
        init();
        setBounds(150,160,280,290);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init() {
        menubar = new JMenuBar();
        menu = new JMenu("编辑");
        itemCopy = new JMenuItem("复制");
        itemCut = new JMenuItem("剪切");
        itemPaste = new JMenuItem("粘贴");
        menu.add(itemCopy);
        menu.add(itemCut);
        menu.add(itemPaste);
        menubar.add(menu);
        setJMenuBar(menubar);
        text = new JTextArea();
        add(new JScrollPane(text), BorderLayout.CENTER);
        itemCopy.addActionListener(this);
        itemCut.addActionListener(this);
        itemPaste.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == itemCopy) {
            text.copy();
        }
        if(e.getSource() == itemCut) {
            text.cut();
        }
        if(e.getSource() == itemPaste) {
            text.paste();
        }
    }
}
